
package coms.ejb;

import coms.dataaccess.dao.UserDAO;
import coms.dataaccess.hibernate.HibernateDAOFactory;
import coms.dto.Conference;
import coms.dto.ConferenceItem;
import coms.dto.User;
import coms.exceptions.DAOException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;


@Stateless (mappedName="UserBean")
public class UserBean extends GenericBean<User> implements UserBeanRemote{

    private UserDAO userDAO;
    
    @PostConstruct
    public void construct(){
        super.construct();
        HibernateDAOFactory factory = new HibernateDAOFactory();
        userDAO = factory.getUserDAO();
    }
    
    @Override
    public User findUserByUsername(String username) throws DAOException {
        return userDAO.findUserByUsername(username);
    }
    
    @Override
    public User validateUser(String username, String password) throws DAOException {
        User usr;
        try {
            System.out.println("username"+ username);
            System.out.println("password"+ password);
            usr = findUserByUsername(username);
            if ( null != usr && null != username && null!= password) {
                   if (username.trim().equalsIgnoreCase(usr.getUsername()) && password.trim().equalsIgnoreCase(usr.getPassword()) )  {
                    return usr;
                }
            }
        } catch (Exception e) {
            System.err.println("DAO Exception in Validate and Find User"+ e);
            throw new DAOException(e);
        }
        
        return usr;
    }

    @Override
    public List<User> searchParticipantByConference(Conference conference) {
        return userDAO.searchParticipantByConference(conference);
    }
    
    @Override
    public List<User> searchParticipantByConferenceItem(ConferenceItem conferenceItem) {
        return userDAO.searchParticipantByConferenceItem(conferenceItem);
    }
    
}
